package com.winchain.chainbill.bill.biz.send.rx;

import com.shell.framework.command.ChainBase;
import com.shell.framework.command.CommandContext;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.bill.base.po.CbAtt;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillEvent;
import com.winchain.chainbill.bill.biz.send.cmd.BillAttCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillInvoiceCmd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillAttSubscriber extends OnErrorNotImplementedSubscriber<BillEvent<? extends Object>> {
	@Autowired
	private BillAttCmd<CbAtt> billAttCmd;
	@Autowired
	private BillInvoiceCmd<CbAtt> billInvoiceCmd;

	public void onApplicationEvent(BillEvent<? extends Object> t) {
		String bizId = (String) t.getValue("bizId");

		String contractFileIds = (String) t.getValue("contractFileIds");
		String invoiceNo = (String) t.getValue("invoiceNo");

		Bill<CbAtt> bill = new Bill();
		bill.setRec(t.getBill().getRec());

		CommandContext<Bill<CbAtt>> context = new CommandContext();
		context.setSource(bill);
		context.setValue("bizId", bizId);

		context.setValue("contractFileIds", contractFileIds);
		context.setValue("invoiceNo", invoiceNo);
		context.setValue("attInvoices", t.getValue("attInvoices"));
		context.setValue("rejectFlag", t.getValue("rejectFlag"));
		context.setValue("invType", t.getValue("invType"));
		context.setValue("billSource", t.getBill().getSource());

		ChainBase<Bill<CbAtt>> crb = new ChainBase();
		crb.add(this.billAttCmd);
		crb.add(this.billInvoiceCmd);
		crb.execute(context);
	}
}
